{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5bab1578-e200-4a2d-8819-1a2ccf12ec77",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "93904077-aa07-41cb-9402-972c862242e0",
   "metadata": {},
   "source": [
    "## 对于ndarray结构来说，里面所有的元素必须是同一类型的如果不是的话，会自动的向下进行转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "474dbc5d-38c5-4921-b11a-33a58e2a97c9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_list = [1,2,3,4,5]\n",
    "liao_array = np.array(liao_list)\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca4cbbd6-8195-4338-9c6c-8cb3b8fb0e7b",
   "metadata": {},
   "source": [
    "### ndarray 基本属性操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "907a67d3-fdd2-490f-ac27-a265c5ced2f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(liao_array)  #数组的类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9ef5d4cc-c9f5-49cd-81c8-d770ec6d75e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.dtype #数组的数据类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "42bf793d-23e0-42cc-ba38-e69bc6bd6da2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.itemsize # 查看数组中每个元素所占的字节数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "92abfb81-38b0-40dd-a4b2-df82721bd78d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.shape # 查看数组的维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "4d75285b-b3c3-48a4-9aed-76cb3c4c4d7d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.size #返回数组中的元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "af28eda5-3caf-4731-8abf-ba274c674e74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.size(liao_array) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "867d7325-1dbc-4f3d-847d-397db68f97d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5,)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.shape(liao_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "8d5de9d1-4123-49ef-b36c-739f3eb6ad9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.ndim  # 返回数组的维度的数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "16087954-b069-4457-beac-7402d38c9954",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2f689e6b-70e4-4ec5-8dd2-e972668b1fea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.fill(0)  #用0替代数组中的所有元素\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "363f8d14-d262-4775-a357-7d744317e2b7",
   "metadata": {},
   "source": [
    "索引与切片和Python一样都是 从 0 开始的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "2ac3da62-caa2-4f1b-a78d-50614063701a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_list = [1,2,3,4,5]\n",
    "liao_array = np.array(liao_list)\n",
    "liao_array[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "9aca7b27-de87-4d11-8130-fbd459bd7459",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array[1:3] ### 用于从数组中提取第 2 3 个元素（左闭右开）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "123a24c2-2fff-4897-bb30-ffcc4d7bcf04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array[-2:] ### 负号表示倒数，即提取倒数第1 倒数第二 个元素"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "689db0c8-ee02-4ab2-a62d-7464aa661456",
   "metadata": {},
   "source": [
    "矩阵格式（多维的形式）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "5ed55eea-f40a-4cd8-b6ef-8a8d8e1a70b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6],\n",
       "       [7, 8, 9]])"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array = np.array([[1,2,3],\n",
    "                      [4,5,6],\n",
    "                      [7,8,9]])\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "373b9fc8-1d90-4e18-8ea7-ce74e35e963a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 3)"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "bb520ad2-5d84-4ce4-8a7b-eaffa7d63eee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "b713f056-36c2-4640-bef5-e893f7e88015",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array.ndim ### 为什么返回2？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "a8d50a94-e3bd-4a4b-b87a-7194f76eaa1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array[1,1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "9c4abb3e-6b9a-47b7-9de8-ab6bd3bad101",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3],\n",
       "       [ 4, 10,  6],\n",
       "       [ 7,  8,  9]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array[1,1] = 10\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "b696e586-df14-4e27-8a91-6cf87718d2bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4, 10,  6])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array[1] ### 矩阵的第一行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "e5f4214b-90d8-44a6-883e-97190fdbe009",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2, 10,  8])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array[:,1]  ### 矩阵的第1列，一定不要忘了逗号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "id": "e680b86f-3a7c-4cc2-9ac0-aec585fc3503",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array[0,0:2] ### 矩阵第0行的第0列，第1列 元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "c8921f8f-4606-48a7-9eed-2a7fb3b4fd33",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3],\n",
       "       [ 4, 10,  6],\n",
       "       [ 7,  8,  9]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "6c8dad49-168f-4dad-bda6-cb4c9a2b45d8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3],\n",
       "       [ 4, 10,  6],\n",
       "       [ 7,  8,  9]])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array2 = liao_array\n",
    "liao_array2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "bedb480e-eb6c-4cc9-8652-dee81dbb7e2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  4, 100,   6],\n",
       "       [  7,   8,   9]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array2[1,1] = 100\n",
    "liao_array2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "8f90c8b5-423b-4537-83d2-7155ac52e72d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  4, 100,   6],\n",
       "       [  7,   8,   9]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1206b92-522b-4842-9331-d1685af51f1f",
   "metadata": {},
   "source": [
    "### 发现这样的话，改动liao_array2， liao_array也被改变了\n",
    "### 如何避免这种状况？ —————— 使用 .copy()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "9fd165f9-cbcf-4747-89b9-5545af31f01a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  4, 100,   6],\n",
       "       [  7,   8,   9]])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array2 = liao_array.copy()\n",
    "liao_array2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "e2793a7f-0c8a-41a7-9768-239bc0ba09f7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[   1,    2,    3],\n",
       "       [   4, 1000,    6],\n",
       "       [   7,    8,    9]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array2[1,1] = 1000\n",
    "liao_array2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "a400a43b-1908-4771-9baa-99b526f4e049",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[  1,   2,   3],\n",
       "       [  4, 100,   6],\n",
       "       [  7,   8,   9]])"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "851aa5ed-c5d8-4a58-ad96-8b8575ce052b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, 10, 20, 30, 40, 50, 60, 70, 80, 90])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array = np.arange(0,100,10) ### 从0到100创建一维数组，间隔是10\n",
    "liao_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "c25d2b86-0178-4c69-8d1e-309d026aae66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False,  True,  True,  True, False, False,  True,\n",
       "        True])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = np.array([0,0,0,1,1,1,0,0,1,1], dtype=bool)  #布尔类型\n",
    "mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "9e79cc2c-31da-4fb7-b7ca-411eb9e96fa6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([30, 40, 50, 80, 90])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "liao_array[mask] ### 取数组array中的元素，如果布尔数组mask中的元素是TURE，就取这个元素，否则不取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "996dca82-eff0-464c-b108-08edaabaa447",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c04d5ba0-673e-4860-a587-88add1ff72e0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
